package edu.jvn.ict.locationsharing.informationexchange;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.TreeMap;
import java.util.Vector;

import edu.jvn.ict.locationsharing.MemberInfo;
import edu.jvn.ict.locationsharing.MemberServer;
import edu.jvn.ict.locationsharing.Utility;

public class SendBackDictinonaryMessage extends ExchangeMessage {

	/**
	 * 
	 */
	public static final int MESSAGE_ID = 7;
	private static final long serialVersionUID = -5038931257815106710L;

	@SuppressWarnings("unchecked")
	@Override
	public void process(ObjectInputStream in, ObjectOutputStream out)
			throws IOException {

		try {
			//Object temp = in.readObject();
			MemberServer.dictionary = (Vector<MemberInfo>) in.readObject();

			MemberServer.map = new TreeMap<Long, MemberInfo>();
			for (MemberInfo memberInfo : MemberServer.dictionary) {
				MemberServer.map.put(memberInfo.getId(), memberInfo);
				if (memberInfo.getIp() == MemberServer.thisMember.getIp() 
						&& memberInfo.getJoiningTimestamp() == MemberServer.thisMember.getJoiningTimestamp()
						&& memberInfo.getPort() == MemberServer.thisMember.getPort()) {
					MemberServer.thisMember = memberInfo;
				}
			}
						
//			MemberServer.startExchangeLocation();
			
			MemberServer.registerSmsReceiver();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public String toString(){
		return Utility.APP_START_MESSAGE + Integer.toString(MESSAGE_ID);
	}
	
	public SendBackDictinonaryMessage(){
		
	}
}
